using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyWebPagesStarterKit.Controls;
using MyWebPagesStarterKit;
using System.IO;
using System.Collections.Generic;

public partial class SectionControls_SectionAdmin : System.Web.UI.UserControl, ISectionAdmin
{
    private SectionControlBaseClass _section;
    private WebPage _page;

    public SectionControlBaseClass SectionControl
    {
        set { _section = value; }
    }

    public WebPage WebPage
    {
        set { _page = value; }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        try
        {
            btnSendToSidebar.Visible = !(Sidebar.GetInstance().ContainsSection(_section.Section.SectionId, _page.PageId)) && (_section.Section is ISidebarObject);
            btnRemoveFromSidebar.Visible = Sidebar.GetInstance().ContainsSection(_section.Section.SectionId, _page.PageId);
            btnToggleViewMode.Visible = _section.HasAdminView;
            chkOnline.Checked = _section.Section.IsOnline;

            // PowerUser Management
            if (!Page.User.IsInRole(RoleNames.Administrators.ToString()))
            {
                btnDeleteSection.Visible = false;
            }

            if (_section.InfoUrl.Equals(""))
            {
                btnInfo.Visible = false;
            }
            else
            {
                btnInfo.OnClientClick = string.Format("window.open('{0}','InfoPopUp', 'height=780,width=800,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no');return false;", _section.InfoUrl);
            }
            if (!IsPostBack)
            {
                litSectionName.Text = _section.Section.GetLocalizedSectionName();
                string WarningText = string.Format(Resources.StringsRes.ResourceManager.GetString("ctl_SectionAdmin_DeleteWarning"), litSectionName.Text);
                btnDeleteSection.OnClientClick = "if(!confirm('" + WarningText + "')) return false;";
            }
        }
        catch { }
    }


    #region Button Events

    protected void ChangeRole(object sender, EventArgs e)
    {
        MyWebPagesStarterKit.Rule rule = MyWebPagesStarterKit.Rule.VIEW;

        foreach(ListItem item in lstRules.Items) 
        {
            switch (item.Value)
            {
                case "VIEW":
                    rule = MyWebPagesStarterKit.Rule.VIEW;
                    break;
                case "EDIT":
                    rule = MyWebPagesStarterKit.Rule.EDIT;
                    break;
                case "ADMIN":
                    rule = MyWebPagesStarterKit.Rule.ADMIN;
                    break;
                case "DENY":
                    rule = MyWebPagesStarterKit.Rule.DENY;
                    break;
            }
            item.Selected = RolesManagement.GetInstance().RoleExit(_section.Section, chkRoles.SelectedValue, rule);
        }
    }

    //Save roles
    protected void SaveRoles(object sender, EventArgs e)
    {
        MyWebPagesStarterKit.Rule rule = MyWebPagesStarterKit.Rule.VIEW;
        RolesManagement manager = RolesManagement.GetInstance();

        switch (lstRules.SelectedValue)
        {
            case "VIEW":
                rule = MyWebPagesStarterKit.Rule.VIEW;
                break;
            case "EDIT":
                rule = MyWebPagesStarterKit.Rule.EDIT;
                break;
            case "ADMIN":
                rule = MyWebPagesStarterKit.Rule.ADMIN;
                break;
            case "DENY":
                rule = MyWebPagesStarterKit.Rule.DENY;
                break;
        }

        manager.AddRole(_section.Section, chkRoles.SelectedValue, rule);
        manager.SaveData();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.User.IsInRole(MyWebPagesStarterKit.RoleNames.Administrators.ToString()))
        {
            if (!IsPostBack)
            {
                foreach (string role in Roles.GetAllRoles())
                {
                    if (!role.Equals("Administrators"))
                    {
                        ListItem item = new ListItem(role, role);
                        chkRoles.Items.Add(item);
                    }
                }
                chkRoles.Items.Add(new ListItem("UnAuthenticated", "UnAuthenticated"));
            }
        }
        else
        {
            btnToggleRolesMode.Visible = false;
            chkOnline.Visible = false;
        }
    }

    protected void RolesView_Click(object sender, EventArgs e)
    {
        multiview.SetActiveView(rolesView);
    }

    protected void AdminView_Click(object sender, EventArgs e)
    {
        multiview.SetActiveView(sectionView);
    }

    protected void UpdateSectionStatus(object sender, EventArgs e)
    {
        _section.Section.IsOnline = chkOnline.Checked;
        _section.Section.SaveData();

        chkOnline.Checked = _section.Section.IsOnline;
    }


    protected void btnDeleteSection_Click(object sender, EventArgs e)
    {
        _page.RemoveSection(_section.Section.SectionId);
        _section.Section.DeleteFolders(_section.Section.SectionId); //Try to remove any folder that belong to this section
        _section.Section.Delete();

        RolesManagement.GetInstance().DeleteRoleData(_section.Section);

        Response.Redirect(Request.RawUrl);
    }

    protected void btnMoveUp_Click(object sender, EventArgs e)
    {
        _page.MoveSectionUp(_section.Section.SectionId);
        Response.Redirect(Request.RawUrl);
    }

    protected void btnMoveDown_Click(object sender, EventArgs e)
    {
        _page.MoveSectionDown(_section.Section.SectionId);
        Response.Redirect(Request.RawUrl);
    }

    protected void btnSendToSidebar_Click(object sender, EventArgs e)
    {
        Sidebar.GetInstance().AddSection(_section.Section.SectionId, _page.PageId);
    }

    protected void btnRemoveFromSidebar_Click(object sender, EventArgs e)
    {
        Sidebar.GetInstance().RemoveSection(_section.Section.SectionId, _page.PageId);
    }

    protected void btnToggleViewMode_Click(object sender, EventArgs e)
    {
        if (_section.ViewMode == ViewMode.Edit)
        {
            _section.ViewMode = ViewMode.Readonly;
            btnToggleViewMode.ImageUrl = "~/Images/Adm/pencil_16.png";
            btnToggleViewMode.ToolTip = Resources.StringsRes.glb__EditMode;
        }
        else
        {
            _section.ViewMode = ViewMode.Edit;
            btnToggleViewMode.ImageUrl = "~/Images/Adm/search_16.png";
            btnToggleViewMode.ToolTip = Resources.StringsRes.glb__ViewMode;
        }
    }

    #endregion
}